package first.libxml.handlers;



import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import first.liter.AbstractLanguage;
import first.liter.BaseLanguage;
import first.liter.Literature;
import first.liter.Literature.LITERATURE;

public class LiteratureHandler implements ContentHandler 
{
	
	enum LiteratureEnum {
		NAMELIT, AUTHOR, DESC, AMOUNTOFPAGES, EDITNUMLIT, AVAILABILITYOFLIT
		}
	
	List<Literature> lit=new ArrayList<Literature>();
	Literature curr=null;
	LiteratureEnum currentEnum=null;
	BaseLanguage bas;
	Literature literature;

	public List<Literature> getLit()
	{
		return lit;
	}
	
	
	public void characters(char[] ch, int start, int length) 
	{
		String s = new String(ch, start, length).trim();
		if(currentEnum == null) return;
		switch (currentEnum) 
		{
			case NAMELIT:
				curr.setNameLit(s);
				break;
			case AUTHOR:
				curr.setAuthor(s);
				break;
			case DESC:
				curr.setDesc(s);
				break;
			case AMOUNTOFPAGES:
				curr.setAmountOfPages(Integer.parseInt(s));
				break;
			case EDITNUMLIT:
				curr.setEditNumLit(Integer.parseInt(s));
				break;
			case AVAILABILITYOFLIT:	
				curr.setAvailabilityOfLit(Boolean.parseBoolean(s));
				break;
		}
	}

	
	public void endElement(String uri, String localName, String qName) 
	{
		if(qName.equals("literature"))
		lit.add(curr);
		currentEnum = null;
	}


	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		// TODO Auto-generated method stub

	}

	public void startDocument() throws SAXException
	{
		System.out.println("Parsing started...\n");

	}

	
	public void startElement(String uri, String localName,String qName, Attributes attrs) 
	{
			if (qName.equals("literature")) 
			{
				bas=AbstractLanguage.getLanguage(attrs.getValue(0));
				if (attrs.getValue(1).toUpperCase().equals("BOOK")) curr=bas.createBooks("");
				if (attrs.getValue(1).toUpperCase().equals("JOURNAL")) curr=bas.createJournals("");
				curr.setIdLit(Integer.parseInt(attrs.getValue(2)));
			}
			
			if(!"literature".equals(qName) && 	!qName.equals("literatures"))
				currentEnum =LiteratureEnum.valueOf(qName.toUpperCase());
	}



	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void setDocumentLocator(Locator arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}	
	

}
